package Elementary_algorithm.Array;

import java.util.HashSet;

/*
有效的数独
请你判断一个9 x 9 的数独是否有效。只需要 根据以下规则 ，验证已经填入的数字是否有效即可。
数字1-9在每一行只能出现一次。
数字1-9在每一列只能出现一次。
数字1-9在每一个以粗实线分隔的3x3宫内只能出现一次。（请参考示例图）
注意：
一个有效的数独（部分已被填充）不一定是可解的。
只需要根据以上规则，验证已经填入的数字是否有效即可。
空白格用'.'表示。

示例 1：
输入：board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出：true
示例 2：

输入：board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出：false
解释：除了第一行的第一个数字从 5 改为 8 以外，空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

作者：LeetCode
链接：https://leetcode.cn/leetbook/read/top-interview-questions-easy/x2f9gg/
 */
public class _10有效的数独 {


    //判断少了两个条件
    //数字1-9在每一行只能出现一次。
    //数字1-9在每一列只能出现一次。
    public boolean isValidSudoku(char[][] board) {

        for (int i = 0; i < 8; i += 3) {
            for (int j = 0; j < 8; j += 3) {
                boolean search = search(board, i, j);
                if (!search) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean search(char[][] board, int row, int col) {
        HashSet<Character> set = new HashSet<Character>();
        for (int i = 0; i < 3; i++) {
            int trow = row;
            int tcol = col;
            tcol += i;
            for (int j = 0; j < 3; j++) {
                char c = board[trow][tcol];
                if (c == '.') {
                    trow ++;
                    continue;
                }else {
                    if (!set.contains(c)) {
                        set.add(c);
                    }else {
                        return false;
                    }
                }
                trow ++;
            }
        }
        return true;
    }

    //官解:一次遍历
    class Solution {
        public boolean isValidSudoku(char[][] board) {
            int[][] rows = new int[9][9];
            int[][] columns = new int[9][9];
            int[][][] subboxes = new int[3][3][9];
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    char c = board[i][j];
                    if (c != '.') {
                        int index = c - '0' - 1;
                        rows[i][index]++;
                        columns[j][index]++;
                        subboxes[i / 3][j / 3][index]++;
                        if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }
}
